package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Button;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.model.GroupModel;
import com.hangzhou.xyj.net.handler.GetGroupsInfoHandler;
import com.hangzhou.xyj.net.handler.JoinGroupHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetGroupsInfoReq;
import com.hangzhou.xyj.net.request.JoinGroupReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.listener.CheckLoginListener;
import com.hangzhou.xyj.ui.widget.CircleImageView;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.List;

/**
 * Created by Administrator on 2016/1/18.
 */
public class GroupDetailActivity extends BaseActivity {
    Intent intent;
    CircleImageView mIvGroupImg;//群头像
    MyTextView mTvGroupName;//群名称
    MyTextView mTvGroupPartNum;//群成员数
    MyTextView mTvGroupId;//群ID
    CircleImageView mCivGroupLeaderImg;//群主头像
    MyTextView mTvGroupLeaderName;//群主昵称
    MyTextView mTvIntroduce;//群简介
    MyTextView mTvGroupCls;//群类别
    MyTextView mTvCreteTime;//群创建时间
    private Button mBtnJoin;

    private String mGroupCode;
    private GroupModel mGroupModel; //当前页面的圈子信息

    @Override
    public void initIntentData(Intent intent) {
        super.initIntentData(intent);
        mGroupCode = intent.getStringExtra(DataKey.GROUP_CODE);
    }

    @Override
    public void initDatas() {
        GetGroupsInfoReq getGroupsInfoReq = new GetGroupsInfoReq(mGroupCode);
        HttpLoader.getInstance().sendRequest(new GetGroupsInfoHandler(getGroupsInfoReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                List<GroupModel> list = (List<GroupModel>) model;
                setData(list.get(0));
            }
        }, 0));
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_group_detail);
    }

    @Override
    public void initListeners() {
        //加入圈子
        findView(R.id.btn_apart_in_group).setOnClickListener(new CheckLoginListener() {
            @Override
            public void onClickView(View v) {
                joinGroup();
            }
        });
    }

    @Override
    public void initTop() {
        enableTitle(true, "");
        enableRefresh(true);
    }

    @Override
    public void initViews() {
        mIvGroupImg = findView(R.id.miv_header);
        mTvGroupName = findView(R.id.mtv_group_name);
        mTvGroupPartNum = findView(R.id.mtv_part_num);
        mTvGroupId = findView(R.id.mtv_group_id);
        mCivGroupLeaderImg = findView(R.id.miv_group_leader_img);
        mTvGroupLeaderName = findView(R.id.mtv_group_leader_name);
        mTvIntroduce = findView(R.id.mtv_group_introduce);
        mTvGroupCls = findView(R.id.mtv_group_cls);
        mTvCreteTime = findView(R.id.mtv_group_create_time);
        mBtnJoin = findView(R.id.btn_apart_in_group);
    }

    public void setData(GroupModel data) {
        this.mGroupModel = data;

        enableTitle(true, data.getGroupName());
        mIvGroupImg.displayImage(data.getGroupImage());//群Icon
        mTvGroupName.setText(data.getGroupName());//群名称
        mTvGroupPartNum.setText(String.valueOf(data.getMemberNum()));//群成员数量
        mTvGroupId.setText(data.getId());
        mCivGroupLeaderImg.displayImage(data.getGroupMasterIcon());
        mTvGroupLeaderName.setText(data.getGroupMasterName());
        mTvIntroduce.setText(data.getGroupDesc());
        mTvGroupCls.setText(data.getGroupCls());
        mTvCreteTime.setText(data.getCreateTime());
    }

    /**
     * 加入圈子
     */
    private void joinGroup() {
        JoinGroupReq req = new JoinGroupReq(XYJApplication.getUserInfo().getUserId(), mGroupModel.getId());
        HttpLoader.getInstance().sendRequest(new JoinGroupHandler(req, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mBtnJoin.setVisibility(View.GONE);
                ToastUtil.show("已提交申请");
            }
        }, 0));
    }
}
